/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.libspml.impl;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.googlecode.libspml.AbstractToken;
import com.googlecode.libspml.ITokenProvider;

/**
 * @author Roger Cockshutt
 *
 */
public class SPMLTokenProvider implements ITokenProvider {
	
	

	/**
	 * 
	 */
	public SPMLTokenProvider() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.googlecode.libspml.ITokenProvider#getTokens(java.lang.String[])
	 */
	@Override
	public List<AbstractToken> getTokens(String... strings) {
		Vector<AbstractToken> tokens = new Vector<AbstractToken>();
		Matcher currentMatcher = null;
		for (String string : strings) {
			for (Type type : Type.values()) {
				currentMatcher = Pattern.compile(type._regex).matcher(string);
				if (currentMatcher.find()) {
					tokens.add(type.makeToken(currentMatcher.group()));
				}
			}
		}
		
		return tokens;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.libspml.ITokenProvider#getTokens(java.io.BufferedReader)
	 */
	@Override
	public List<AbstractToken> getTokens(BufferedReader reader) {
		String lines = "";
		try {
			String newLine = reader.readLine();
			while (newLine != null) {
				lines += newLine;
				newLine = reader.readLine();
			}
		} catch (IOException exception) {
			// TODO
			exception.printStackTrace();
		}
		
		
		return getTokens(lines.split("\\s+"));
	}

	/* (non-Javadoc)
	 * @see com.googlecode.libspml.ITokenProvider#getTokens(java.nio.file.Path)
	 */
	@Override
	public List<AbstractToken> getTokens(String filePath) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(filePath));
		} catch (FileNotFoundException exception) {
			// TODO
			exception.printStackTrace();
		}
		
		return getTokens(reader);
	}

}
